package com.github.zyf.ioaj.export;

import com.github.zyf.ioaj.exception.ContentBuilderException;
import com.github.zyf.ioaj.exception.constant.ContentBuilderExceptionType;

import java.util.Arrays;

/**
 * <h1>Json内容加载器</h1>
 * <p>继承了{@link ContentBuilder}，用于加载Json内容</p>
 *
 * @author zyf
 */
public class JsonContentBuilder extends ContentBuilder {

    private final StringBuilder stringBuilder;

    public JsonContentBuilder(String title, String author, String message) {
        super(title, author, message);
        stringBuilder = new StringBuilder("{");
        stringBuilder.append("\"title\": \"").append(title).append("\",");
        stringBuilder.append("\"author\": \"").append(author).append("\",");
        stringBuilder.append("\"message\": \"").append(message).append("\",");
        stringBuilder.append("\"details\": {");
    }

    @Override
    public String toString() {
        if (isCompleted()) return stringBuilder.toString();
        else throw new ContentBuilderException(ContentBuilderExceptionType.INCOMPLETE_CONTENT);
    }

    @Override
    public void complete(Class<?> clazz) {
        stringBuilder.append("\"class\": \"").append(clazz.getName()).append("\"}}");
        setCompleted();
    }

    @Override
    public void appendString(String key, String desc, String value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": \"").append(value).append("\"")
                .append("},");
    }

    @Override
    public void appendStrings_1(String key, String desc, String[] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": \"").append(Arrays.toString(value)).append("\"")
                .append("},");
    }

    @Override
    public void appendStrings_2(String key, String desc, String[][] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": \"").append(Arrays.deepToString(value)).append("\"")
                .append("},");
    }

    @Override
    public void appendBoolean(String key, String desc, boolean value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(value)
                .append("},");
    }

    @Override
    public void appendInt(String key, String desc, int value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(value)
                .append("},");
    }

    @Override
    public void appendInts_1(String key, String desc, int[] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(Arrays.toString(value))
                .append("},");
    }

    @Override
    public void appendInts_2(String key, String desc, int[][] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(Arrays.deepToString(value))
                .append("},");
    }

    @Override
    public void appendDouble(String key, String desc, double value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(value)
                .append("},");
    }

    @Override
    public void appendDoubles_1(String key, String desc, double[] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(Arrays.toString(value))
                .append("},");
    }

    @Override
    public void appendDoubles_2(String key, String desc, double[][] value) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": ").append(Arrays.deepToString(value))
                .append("},");
    }

    @Override
    public void appendObjectHeader(String key, String desc) {
        stringBuilder.append("\"").append(key).append("\": {")
                .append("\"desc\": \"").append(desc).append("\",")
                .append("\"value\": {");
    }

    @Override
    public void appendObjectEnd(Object o) {
        stringBuilder.append("\"class\": \"").append(o.getClass().getName()).append("\"}},");
    }

    @Override
    public void appendNewNodeHeader(String nodename, String desc) {
        stringBuilder.append("{\"").append(nodename).append("\": {")
                .append("\"desc\": \"").append(desc).append("\" ,")
                .append("\"value\": {");
    }

    @Override
    public void appendNewNodeEnd(String nodename) {
        stringBuilder.append("\"node\": \"").append(nodename).append("\"}}},");
    }

    @Override
    public void appendNewArrayHeader(String key) {
        stringBuilder.append("\"").append(key).append("\": [");
    }

    @Override
    public void appendNewArrayEnd(String key) {
        stringBuilder.append("{\"array\": \"").append(key).append("\"}],");
    }
}
